package com.ls.lsqc.modules.sys.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lsqc.common.utils.CacheUtils;
import com.ls.lsqc.common.utils.SpringContextHolder;
import com.ls.lsqc.modules.order.entity.Order;
import com.ls.lsqc.modules.sys.dao.QualityDao;

import java.util.List;
import java.util.Map;


/**
 * 设计项目工具类
 *
 * @author 于辉
 * @version 2017-06-16
 */
public class QualityUtils {

	private static QualityDao qualityDao = SpringContextHolder.getBean(QualityDao.class);

	public static final String CACHE_QUALITY_VALUE_MAP_34 = "qualityValueMap34";
	public static final String CACHE_QUALITY_VALUE_MAP_35 = "qualityValueMap35";
	public static final String CACHE_QUALITY_VALUE_MAP_36 = "qualityValueMap36";
	public static final String CACHE_QUALITY_VALUE_MAP_37 = "qualityValueMap37";

	/**
	 * @Title: putQualityCache
	 * @Description: 质检项目全量放入缓存
	 * @author 于辉
	 *
	 */
	public static void putQualityCache() {
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap34 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_34);
		if (qualityMap34 == null) {
			qualityMap34 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),34L)) {
				List<Order> dictList = qualityMap34.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap34.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap34.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_34, qualityMap34);
		}
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap35 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_35);
		if (qualityMap35 == null) {
			qualityMap35 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),35L)) {
				List<Order> dictList = qualityMap35.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap35.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap35.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_35, qualityMap35);
		}
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap36 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_36);
		if (qualityMap36 == null) {
			qualityMap36 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),36L)) {
				List<Order> dictList = qualityMap36.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap36.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap36.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_36, qualityMap36);
		}
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap37 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_37);
		if (qualityMap37 == null) {
			qualityMap37 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),37L)) {
				List<Order> dictList = qualityMap37.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap37.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap37.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_37, qualityMap37);
		}
	}
	
	/**
     * 获取订单基本信息
     * @param 
     * @return
     */
    public static List<Order> findAllList(Order order,Long operatingId){
    	if (operatingId!=null) {
    		order.setOperatingId(operatingId);
    		List<Order> orders = qualityDao.findAllOrderList(order);
    		for (Order orz:orders) {
    			if (orz!=null) {
    				orz.setStyleOpList(qualityDao.findStyleControl(orz.getStyleNo(),operatingId+""));
    			}
    		}
    		return orders;
		}
		return null;
    }
}
